
#include <BReadWrite>
#include <BHBoxLayout>
#include <BStyle>
#include <BImage>

#include "ResourceDialog.h"

ResourceDialog::ResourceDialog(BWidget* parent) : BDialog(parent)
{
	this->setSize(500, 500);
	this->setTitle("Application Resources");
	this->setStyleSheet("style:teal");

	_treeWidget = new BTreeWidget();
	_listWidget = new BListWidget();

	BHBoxLayout* hlayout = new BHBoxLayout(this);
	hlayout->addWidget(_treeWidget);
	hlayout->addWidget(_listWidget);

}
ResourceDialog::~ResourceDialog()
{

}

void ResourceDialog::freshTreeItem(BTreeItem* item)
{
	if (item->childCount() > 0)
	{
		const BStyle* style = this->realStyle();
		BImage* folderImage = style->image("folder");
		item->setImage(folderImage);
	}
	for (int i = 0; i < item->childCount(); i++)
	{
		BTreeItem* child = item->child(i);
		freshTreeItem(child);
	}
}

void ResourceDialog::showEvent(const BEvent& event)
{
	BDialog::showEvent(event);
	_treeWidget->clearItems();
	_listWidget->clearItems();
	int count = GetResourceCount();
	for (int i = 0; i < count; i++)
	{
		const BString& name = GetResourceName(i);
		BImage* image = ReadFile<BImage>(name);
		_treeWidget->create(name)->setImage(image);
		_listWidget->addItem(name)->setImage(image);
	}
	for (int i = 0; i < _treeWidget->itemCount(); i++)
	{
		freshTreeItem(_treeWidget->item(i));
	}
}

